package com.demo.cms.action.user;

import org.apache.commons.lang3.StringUtils;

import com.demo.cms.service.ArticleCommentService;
import com.demo.cms.service.ArticleService;
import com.demo.cms.service.CategoryService;
import com.demo.cms.service.ChannelService;
import com.demo.core.model.Member;
import com.demo.core.model.Site;
import com.demo.core.model.Template;
import com.demo.core.model.User;
import com.demo.core.service.base.TemplateService;
import com.demo.security.util.SecurityUtil;
import com.demo.webapp.action.PageAction;

/**
 * CMS的前台访问基础Action
 * <br/>CMS前台用户访问的基础Action
 * @author ryuu.kk
 */
public abstract class ClientBaseAction extends PageAction {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = 706691318449464554L;

	/**
	 * ID
	 */
	private Long id;
	
	/**
	 * 频道ID
	 */
	private Long channelId;
	
	/**
	 * 栏目id
	 */
	private Long categoryId;
	
	/**
	 * 频道地址
	 */
	private String channelPath;
	
	/**
	 * 栏目地址
	 */
	private String categoryPath;

	/**
	 * 基础路径
	 */
	private String base;
	
	/**
	 * 资源路径
	 */
	private String res;
	
	/**
	 * 登录用户
	 */
	private User user;
	
	/**
	 * 会员模型
	 */
	private Member member;
	
	/**
	 * 文章服务
	 */
	private ArticleService articleService;
	
	/**
	 * 文章评论服务接口
	 */
	private ArticleCommentService articleCommentService;
	
	/**
	 * 栏目服务
	 */
	private CategoryService categoryService;
	
	/**
	 * 频道服务
	 */
	private ChannelService channelService;
	
	/**
	 * 模板服务接口
	 */
	private TemplateService templateService;

	/**
	 * 默认执行方法
	 * @return String
	 */
	public void prepare() {
		
		// ~~ 取得当前登录用户(如果已登录)
		user = (User) SecurityUtil.getCurrentUser();
		// ~~ 取得当前登录会员(如果存在)
		Object objModel = SecurityUtil.getPlatformModel();
		if (objModel instanceof Member) {
			member = (Member) objModel;
		}
		// ~~ 取得基础路径
		base = getServletContext().getRealPath("/");
		// ~~ 取得资源路径
		res = getResPath(getSite());
	}
	/**
	 * 返回Action类型
	 * @return String
	 */
	abstract String getActionType();
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public Long getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Long categoryId) {
		this.categoryId = categoryId;
	}

	public Long getChannelId() {
		return channelId;
	}

	public void setChannelId(Long channelId) {
		this.channelId = channelId;
	}

	public String getCategoryPath() {
		return categoryPath;
	}

	public void setCategoryPath(String categoryPath) {
		this.categoryPath = categoryPath;
	}

	public String getChannelPath() {
		return channelPath;
	}

	public void setChannelPath(String channelPath) {
		this.channelPath = channelPath;
	}

	public void setTemplateService(TemplateService templateService) {
		this.templateService = templateService;
	}
	
	/**
	 * 获得url后缀
	 * @param site 站点模型
	 * @param defaultValue 默认值
	 * @return String
	 */
	public String getSuffix(Site site, String defaultValue) {
    	String suffix = site.getSuffix();
    	if (StringUtils.isNotEmpty(suffix)) {
    		if (suffix.charAt(0) != '.') {
    			suffix = '.' + suffix;
    		}
    	} else {
    		// 使用默认后缀
    		suffix = defaultValue;
    	}
    	return suffix;
	}
	
	public User getUser() {
		return user;
	}
	
	public Member getMember() {
		return member;
	}

	/**
	 * 获得url后缀
	 * @return String
	 */
	public String getSuffix() {
		return getSuffix(getSite(), ".html");
	}

	public String getBase() {
		return base;
	}

	public String getRes() {
		return res;
	}
	
	// ~~ 受保护方法   =====================================
	/**
	 * 找到指定模板
	 * @param id 模板ID
	 * @return Template模板
	 */
	protected Template findTemplate(Long id) {
		return templateService.find(id);
	}
	
	/**
	 * 取得站点资源路径
	 * @param site Site
	 * @return String
	 */
	protected String getResPath(Site site) {
		String resPath = site.getResPath();
		if (StringUtils.isEmpty(resPath)) {
			resPath = (String) getConfiguration().get(com.demo.webapp.config.Constants.PUBLISH_FOLDER_KEY);
		}
		resPath.replace("\\", "/");
		if (resPath.charAt(0) != '/' ) {
			resPath = "/" + resPath;
		}
//		if (resPath.charAt(resPath.length() - 1) != '/') {
//			resPath += "/";
//		}
		return resPath;
	}
	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}
	
	public ArticleService getArticleService() {
		return articleService;
	}
	
	public ArticleCommentService getArticleCommentService() {
		return articleCommentService;
	}
	
	public void setArticleCommentService(ArticleCommentService articleCommentService) {
		this.articleCommentService = articleCommentService;
	}
	
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}
	
	public ChannelService getChannelService() {
		return channelService;
	}
	
	public void setChannelService(ChannelService channelService) {
		this.channelService = channelService;
	}
}
